package com.baidu.user.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;

import com.baidu.pojo.App;
import com.baidu.pojo.Pic;
public class runThread extends Thread {
	
			private int startPage;
			private int endPage;
			private AppService service;
	
				
				public runThread(int startPage, int endPage, AppService service) {
				super();
				this.startPage = startPage;
				this.endPage = endPage;
				this.service=service;
			}



				public void run(){
					
					try {
						
						
						for (int i = startPage; i <endPage; i++) {
							
							Parser parser=new Parser("http://mm.10086.cn/android/software/qbrj?pay=1&p="+i);
							parser.setEncoding("UTF-8");
							HasAttributeFilter attributeFilter=new HasAttributeFilter("class","autoword");
							NodeList nodeList = parser.parse(attributeFilter);
							
							for (int j = 0; j < nodeList.size(); j++) {
								String href ="http://mm.10086.cn"+ ((TagNode) nodeList.elementAt(j)).getAttribute("href");
								System.out.println(href);
								
								
								App app =HtmlParser.getAppHtml(href);
								if(app!=null){
									service.insertSelective(app);
								}
							
								List imageList=new ArrayList();
								Map map = HtmlParser.getPicHtml(href);
								for (int k = 1; k<= map.size(); k++) {
									String pickey=("pic"+k).toString();
									service.insertSelective( (Pic) map.get(pickey));
									imageList.add(((Pic) map.get(pickey)).getPicurl());
								}
							
							
								FreeMarkerUtil ful=new FreeMarkerUtil();
								Map<String,Object> root = new HashMap<String,Object>();
								root.put("app", app);
								root.put("imageList", imageList);
								root.put("basePath","");
								ful.print("detailed.ftl",root);
								ful.fprint("detailed.ftl", root, "D:\\apache-tomcat-7.0.42\\app\\"+UUID.randomUUID()+".html");
							}
							
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}



			
}
